//
//  NumberCallController.m
//  20240823
//
//  Created by 张伟 on 2024/8/24.
//

#import "NumberCallController.h"

@interface NumberCallController ()
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *numbers;
@property (nonatomic) NSMutableArray * clickedNumbers;
@property (weak, nonatomic) IBOutlet UITextField *numberTextField;
@property (weak, nonatomic) IBOutlet UIView *deleteView;
@property (weak, nonatomic) IBOutlet UIButton *callBtn;

@end

@implementation NumberCallController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

-(void)setupUI{
    
    [self.view layoutIfNeeded];
    [self.view setNeedsLayout];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (int i = 0; i < self.numbers.count; i++) {
            UILabel * obj = self.numbers[i];
            obj.layer.cornerRadius = obj.frame.size.width / 2;
            obj.layer.masksToBounds = true;
            [obj addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(numberTapAction:)]];
            obj.userInteractionEnabled = true;
            obj.font = obj.tag == 999 ? [UIFont systemFontOfSize:25] : [UIFont systemFontOfSize:35];
            obj.backgroundColor = [UIColor colorWithHexString:@"DBDBDB"];
        }
    });
    
    [_deleteView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteNumberAction:)]];
    [_callBtn setTitle:localizedStringKey(@"ring") forState:UIControlStateNormal];
}

-(void)numberTapAction:(UITapGestureRecognizer *)sender {
    UILabel * obj = (UILabel *) sender.view;
    [self.clickedNumbers addObject:obj.text];
    NSString * str = [self.clickedNumbers componentsJoinedByString:@""];
    _numberTextField.text = str;
}

-(void)deleteNumberAction:(UITapGestureRecognizer *)sender {
    [_clickedNumbers removeLastObject];
    NSString * str = [self.clickedNumbers componentsJoinedByString:@""];
    _numberTextField.text = str;
}

- (NSMutableArray *)clickedNumbers {
    if (!_clickedNumbers) {
        _clickedNumbers = NSMutableArray.array;
    }
    return _clickedNumbers;;
}


- (IBAction)callNumberAction:(UIButton *)sender {
    [PhoneCallManager callPhoneNumber:_numberTextField.text];
}


@end
